package com.hand.demo.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hand.demo.app.service.HhrTaProjExpService;
import com.hand.demo.domain.entity.HhrTaProjExp;
import com.hand.demo.domain.repository.HhrTaProjExpRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 项目经历表(HhrTaProjExp)表控制层
 *
 * @author
 * @since 2025-06-16 12:16:52
 */

@RestController("hhrTaProjExpController.v1")
@RequestMapping("/v1/{organizationId}/hhr-ta-proj-exps")
public class HhrTaProjExpController extends BaseController {

    @Autowired
    private HhrTaProjExpRepository hhrTaProjExpRepository;

    @Autowired
    private HhrTaProjExpService hhrTaProjExpService;

    @ApiOperation(value = "项目经历表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<HhrTaProjExp>> list(HhrTaProjExp hhrTaProjExp, @PathVariable Long organizationId,
                                                   @ApiIgnore @SortDefault(value = HhrTaProjExp.FIELD_ID,
                                                           direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<HhrTaProjExp> list = hhrTaProjExpService.selectList(pageRequest, hhrTaProjExp);
        return Results.success(list);
    }

    @ApiOperation(value = "项目经历表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}/detail")
    public ResponseEntity<HhrTaProjExp> detail(@PathVariable Long id) {
        HhrTaProjExp hhrTaProjExp = hhrTaProjExpRepository.selectByPrimary(id);
        return Results.success(hhrTaProjExp);
    }

    @ApiOperation(value = "创建或更新项目经历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<HhrTaProjExp>> save(@PathVariable Long organizationId, @RequestBody List<HhrTaProjExp> hhrTaProjExps) {
        validObject(hhrTaProjExps);
        SecurityTokenHelper.validTokenIgnoreInsert(hhrTaProjExps);
        hhrTaProjExps.forEach(item -> item.setTenantId(organizationId));
        hhrTaProjExpService.saveData(hhrTaProjExps);
        return Results.success(hhrTaProjExps);
    }

    @ApiOperation(value = "删除项目经历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<HhrTaProjExp> hhrTaProjExps) {
        SecurityTokenHelper.validToken(hhrTaProjExps);
        hhrTaProjExpRepository.batchDeleteByPrimaryKey(hhrTaProjExps);
        return Results.success();
    }

}

